package lagou.work.model1;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Controller {
    public static Integer isFind;
    public static String isContinue;

    /**
     * 判断流程输入值是否合理
     *
     * @param scanner
     * @return
     */
    public static String getString(Scanner scanner) {
        String isContinue;
        while (true) {
            String s = scanner.next();
            if ("Y".equals(s) || "N".equals(s)) {
                isContinue = s;
                break;
            } else {
                System.out.println("输入不合法，请重新输入！");
            }
        }
        return isContinue;
    }


    /**
     * 新增学生对象,考虑学号赋值是否合理，是否重复
     *
     * @param list
     * @return
     * @throws IdException
     */
    public static List<Student> add(List<Student> list) throws IdException, AgeException {
        //控制是否继续新增学生
        String isContinue = "Y";
        //学号是否有人使用
        Integer isUsed = 0;
        while ("Y".equals(isContinue)) {
            Scanner scanner = new Scanner(System.in);
            Student student = new Student();
            System.out.println("请输入您要添加的学号：");
            Integer id = scanner.nextInt();
            //判断学号是否已经有学生使用
            for (int i = 0; i < list.size(); i++) {
                Student student1 = list.get(i);
                if (student1.getId().equals(id)) {
                    isUsed = 1;
                    break;
                }
            }
            if (1 == isUsed) {
                System.out.println("您添加的学号已有学生使用，请重新输入!");
            } else {
                student.setId(id);
                System.out.println("请输入您要添加的姓名：");
                student.setName(scanner.next());
                System.out.println("请输入您要添加的年龄：");
                student.setAge(scanner.nextInt());
                list.add(student);
                System.out.println(student.toString());
                System.out.println("添加成功，是否继续新增？Y/N");
                isContinue = getString(scanner);
            }
            isUsed = 0;
        }
        System.out.println("已退出当前添加功能");
        return list;
    }

    /**
     * 删除学生对象
     *
     * @param list
     * @return
     * @throws IdException
     */
    public static List<Student> delete(List<Student> list) throws IdException {
        String isContinue = "Y";
        Integer isDel = 0;
        while ("Y".equals(isContinue)) {
            System.out.println("请输入您要删除学生的方式：");
            System.out.println("       1.按学号删除       ");
            System.out.println("       2.按姓名删除       ");
            Scanner scanner = new Scanner(System.in);
            switch (scanner.nextInt()) {
                //按照学生学号进行删除
                case 1:
                    System.out.println("请输入待删除学生的学号：");
                    Integer id = scanner.nextInt();
                    if (id < 0) {
                        throw new IdException("您所输入的学号不合理，请重新输入");
                    }
                    for (int i = 0; i < list.size(); i++) {
                        Student student = list.get(i);
                        if (id.equals(student.getId())) {
                            list.remove(student);
                            System.out.println("学号为" + id + "，姓名为" + student.getName() + "，年龄为" + student.getAge() + "的学生已经删除");
                            isDel = 1;
                        }
                    }
                    if (0 == isDel) {
                        System.out.println("学号为" + id + "的学生不存在，请重新输入!");
                    }
                    System.out.println("是否继续删除？Y/N");
                    isContinue = getString(scanner);
                    break;
                //按照学生姓名删除
                case 2:
                    System.out.println("请输入待删除学生的姓名：");
                    String name = scanner.next();
                    for (int i = 0; i < list.size(); i++) {
                        Student student = list.get(i);
                        if (name.equals(student.getName())) {
                            list.remove(student);
                            System.out.println("姓名为" + name + "，学号为" + student.getId() + "，年龄为" + student.getAge() + "的学生已经删除");
                            isDel = 1;
                        }
                    }
                    if (0 == isDel) {
                        System.out.println("姓名为" + name + "的学生不存在");
                    }
                    System.out.println("是否继续删除？Y/N");
                    isContinue = getString(scanner);

                    break;
                default:
                    break;
            }
            isDel = 0;
        }
        System.out.println("已退出当前删除功能");
        return list;
    }


    /**
     * 查找学生
     *
     * @param list
     * @return
     */
    public static List<Student> find(List<Student> list) {
        //是否继续
        isContinue = "Y";
        //是否查找到
        isFind = 0;
        while ("Y" == isContinue) {
            System.out.println("请输入您要查找学生的方式：");
            System.out.println("       1.按学号查找       ");
            System.out.println("       2.按姓名查找       ");
            System.out.println("       3.结束退出");
            Scanner scanner = new Scanner(System.in);
            switch (scanner.nextInt()) {
                case 1:
                    String isContinue2 = "Y";
                    //判断是否接着查找
                    while ("Y".equals(isContinue2)) {
                        System.out.println("请输入您要查找的学生的学号：");
                        Integer id = scanner.nextInt();
                        findStudentById(id, list);
                        System.out.println("是否继续查找？Y/N");
                        isContinue2 = getString(scanner);
                    }
                    break;
                case 2:
                    String isContinue3 = "Y";
                    //判断是否接着查找
                    while ("Y".equals(isContinue3)) {
                        System.out.println("请输入您要查找的学生的姓名：");
                        String name = scanner.next();
                        findStudentByName(name, list);
                        System.out.println("是否继续查找？Y/N");
                        isContinue3 = getString(scanner);
                    }
                    break;
                case 3:
                    return list;
                default:
                    break;
            }
            isFind = 0;
        }
        return list;
    }

    /**
     * 根据id查找学生，封装起来是为了后续修改学生信息复用
     *
     * @param id
     * @param list
     */
    public static Integer findStudentById(Integer id, List<Student> list) {
        isFind = 0;
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (id.equals(student.getId())) {
                System.out.println("学号为" + id + "的学生，姓名为" + student.getName() + "，年龄为" + student.getAge());
                isFind = 1;
                return i;
            }
        }
        if (0 == isFind) {
            System.out.println("学号为" + id + "的学生不存在");
        }
        return null;
    }

    /**
     * 根据姓名查找学生信息，封装起来修改学生信息时复用
     *
     * @param name
     * @param list
     */
    public static Integer findStudentByName(String name, List<Student> list) {
        isFind = 0;
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (name.equals(student.getName())) {
                System.out.println("姓名为" + name + "的学生，学号为" + student.getId() + "，年龄为" + student.getAge());
                isFind = 1;
                return i;
            }
        }
        if (0 == isFind) {
            System.out.println("姓名为" + name + "的学生不存在");
        }
        return null;
    }


    /**
     * 修改学生信息
     *
     * @param list
     * @return
     */
    public static List<Student> modify(List<Student> list) throws IdException, AgeException {
        //是否继续
        String isContinue = "Y";
        //是否修改
        Integer isModified;
        while ("Y".equals(isContinue)) {
            System.out.println("请选择您要修改学生信息的方式：");
            System.out.println("    1.按学号查找并修改     ");
            System.out.println("    2.按姓名查找并修改     ");
            Scanner scanner = new Scanner(System.in);
            switch (scanner.nextInt()) {
                case 1:
                    String isContinue2 = "Y";
                    Integer listId = 0;
                    System.out.println("请输入您要查找的学生的学号：");
                    //学号
                    Integer id = scanner.nextInt();
                    //位于list中的id
                    listId = findStudentById(id, list);
                    if (null != listId) {
                        System.out.println("请输入您要修改的信息类型：");
                        System.out.println("       1.修改学号      ");
                        System.out.println("       2.修改姓名      ");
                        System.out.println("       3.修改年龄      ");
                        Integer type = scanner.nextInt();
                        switch (type) {
                            case 1:
                                modifyStudentId(scanner, list, listId);
                                break;
                            case 2:
                                modifyStudentName(scanner, list, listId);
                                break;
                            case 3:
                                modifyStudentAge(scanner, list, listId);
                                break;
                            default:
                                break;

                        }
                    }
                    break;
                case 2:
                    System.out.println("请输入您要查找的学生的姓名：");
                    //学号
                    String name = scanner.next();
                    //位于list中的id
                    Integer listId2 = findStudentByName(name, list);
                    if (null != listId2) {
                        System.out.println("请输入您要修改的信息类型：");
                        System.out.println("       1.修改学号      ");
                        System.out.println("       2.修改姓名      ");
                        System.out.println("       3.修改年龄      ");
                        Integer type2 = scanner.nextInt();
                        switch (type2) {
                            case 1:
                                modifyStudentId(scanner, list, listId2);
                                break;
                            case 2:
                                modifyStudentName(scanner, list, listId2);
                                break;
                            case 3:
                                modifyStudentAge(scanner, list, listId2);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                default:
                    break;
            }
        }

        return list;
    }

    /**
     * 根据listId修改学号
     *
     * @param scanner
     * @param list
     * @param listId
     * @throws IdException
     */
    public static void modifyStudentId(Scanner scanner, List<Student> list, Integer listId) throws IdException {
        isContinue = "Y";
        //修改后的学号
        Integer IdChanged = 0;
        while ("Y".equals(isContinue)) {
            isFind = 1;
            System.out.println("请输入您要修改后的学号:");
            Integer Id = scanner.nextInt();
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                if (Id.equals(student.getId())) {
                    System.out.println("当前学号已存在，请输入其它学号！");
                    isFind = 0;
                }
            }
            if (isFind != 0) {
                IdChanged = Id;
                break;
            }

        }
        Student student = list.get(listId);
        student.setId(IdChanged);
        System.out.println("修改后的学生信息为，学号：" + student.getId() + "，姓名：" + student.getName() + "，年龄：" + student.getAge());

    }


    /**
     * 修改姓名，姓名允许重名，所以不做其它校验
     *
     * @param scanner
     * @param list
     * @param listId
     */
    public static void modifyStudentName(Scanner scanner, List<Student> list, Integer listId) {
        System.out.println("请输入您要修改后的姓名:");
        String nameChanged = scanner.next();
        Student student = list.get(listId);
        student.setName(nameChanged);
        System.out.println("修改后的学生信息为，学号：" + student.getId() + "，姓名：" + student.getName() + "，年龄：" + student.getAge());
    }

    /**
     * 根据listId修改年龄
     *
     * @param scanner
     * @param list
     * @param listId
     * @throws AgeException
     */
    public static void modifyStudentAge(Scanner scanner, List<Student> list, Integer listId) throws AgeException {
        System.out.println("请输入您要修改后的年龄:");
        Integer ageChanged = scanner.nextInt();
        Student student = list.get(listId);
        student.setAge(ageChanged);
        System.out.println("修改后的学生信息为，学号：" + student.getId() + "，姓名：" + student.getName() + "，年龄：" + student.getAge());
    }


    public static String getStudents(List<Student> list) {
        StringBuilder stringBuilder = new StringBuilder();
        for (Student student : list) {
            stringBuilder.append(student.toString());
            stringBuilder.append("\n");
        }
        return stringBuilder.toString();
    }

    public static void output(List<Student> list) {
        ObjectOutputStream os = null;
        File file = new File("G:\\student.txt");
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            os = new ObjectOutputStream(new FileOutputStream(file));
            os.writeObject(list);
            System.out.println("学生信息输出成功!");
        } catch (IOException e) {
            e.printStackTrace();

        } finally {
            // 4.关闭流对象并释放有关的资源
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    public static List<Student> input(List<Student> list) {
        ObjectInputStream ois = null;
        File file = new File("G:\\student.txt");
        try {
            //判断文件是否存在，存在则读取
            if (file.exists()) {
                ois = new ObjectInputStream(new FileInputStream(file));
                // 从输入流中读取若干个对象（通过list）并打印
                list = (List<Student>)ois.readObject();
                System.out.println("读取学生信息文件成功!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != ois) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }
}
